﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OAuth2.Framework;
using System.Collections.Specialized;
using OAuth2.Provider.Domain;

namespace OAuth2.Provider.AuthorizationEndpoint
{
    public class AuthorizationContext:IAuthorizationContext
    {

        public Domain.AuthorizationGrantBase AuthorizationGrant { get; set; }

        public string ResourceOwnerUsername { get; set; }

        public IClient Client { get; set; }

        public Uri RedirectUri { get; set; }

        public string[] Scope { get; set; }

        public IToken Token { get; set; }

        public NameValueCollection Headers { get; set; }

        public NameValueCollection Form { get; set; }

        public NameValueCollection QueryString { get; set; }

        public ErrorResponse Error { get; set; }

        public AuthorizationContext() { }

        public AuthorizationContext GetContext(NameValueCollection querystring)
        {
            return new AuthorizationContext()
            {
                Client = new ClientBase(querystring[Parameters.ClientId], querystring[Parameters.ClientSecret]),
                RedirectUri = querystring[Parameters.RedirectUri] == null ? null : new Uri(querystring[Parameters.RedirectUri], UriKind.Absolute),
                AuthorizationGrant = new AuthorizationGrantBase() { 
                     ResponseType=querystring[Parameters.ResponseType],
                      State=querystring[Parameters.State]
                },
                Scope = querystring[Parameters.Scope] == null ? new string[0] : querystring[Parameters.Scope].Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries),
                QueryString=querystring
            };
        }

        

    }
}
